package com.yx.coredata.rabbitmq;


import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.yx.coredata.nettySocketIo.EventListennter;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.Date;

@Component
public class Receiver {
    @Autowired
    private EventListennter eventListennter;

    @RabbitListener(queues = "q1")
    @RabbitHandler
    public void process(String notice, Channel channel, Message message) throws Exception{
        System.out.println("q1 Receiver: " + notice +"  at time:"+new Date());
        try {
            MessageVo vo = JSON.parseObject(notice, MessageVo.class);
            if(vo.getType() == 1){
                eventListennter.sendAllUser();
            }
//            //todo 业务逻辑
//            File file = new File("c:\123.txt");
//            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
//            bufferedInputStream.read();

            //告诉服务器收到这条消息 已经被我消费了 可以在队列删掉 这样以后就不会再发了 否则消息服务器以为这条消息没处理掉 后续还会在发
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
            //丢弃这条消息,如果最后一个参数改为true,消息就重新加入队列了
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false);
            System.out.println("receiver fail,message="+notice);
        }
    }

}

